/******************************************************************************
@file:  GeekHomeScreen.java
@author Ramesh Anandhan

DESCRIPTION
   Geekwave home screen.
-----------------------------------------------------------------------------
Copyright (c) 2011, GEEKWAVE SOLUTIONS INC.

All rights reserved.
******************************************************************************/

package com.gkwave.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/*
 * Home screen
 */
public class GeekHomeScreen extends Activity implements OnItemClickListener{
	private final String TAG = "GeekHomeScreen";
	private final int TRANSPORT = 0x00;
	
	private GridView mGrid;
	private ArrayList<Appinfo> mApps = new ArrayList<Appinfo>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.home);
		loadApps();
		mGrid = (GridView)findViewById(R.id.myGrid);
		mGrid.setAdapter(new AppsAdapter(getApplicationContext()));
		mGrid.setOnItemClickListener(this);
	}
	
   /*
    * class hold all application infos
    */
   public class Appinfo{
	   public String mAppName;
	   public Drawable mAppImage;
	   
	   Appinfo(int stringID, int imageID){
		   mAppName = getResources().getString(stringID);
		   mAppImage = getResources().getDrawable(imageID);
	   }
	   Appinfo(){
	   }
   }
	
   /*
    * Adapter represents the data to be displayed by grid view
    */
   public class AppsAdapter extends BaseAdapter {
	    private final LayoutInflater mInflater;
        public AppsAdapter(Context context) {
        	mInflater = LayoutInflater.from(context);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.application_box, parent, false);
            }
            TextView textView = (TextView) convertView;
            textView.setTextColor(Color.BLACK);
            Appinfo info = mApps.get(position);
            textView.setText(info.mAppName);
            textView.setCompoundDrawablesWithIntrinsicBounds(null, info.mAppImage, null, null);
            return convertView;
        }

        public final int getCount() {
            return mApps.size();
        }

        public final Object getItem(int position) {
            return mApps.get(position);
        }

        public final long getItemId(int position) {
            return position;
        }
    }
   
   /*
    * input all application details
    */
   private void loadApps() {
	   Appinfo app_transport = new Appinfo(R.string.app_transport, R.drawable.travel_bus);
	   Appinfo app_tastit = new Appinfo(R.string.app_foodmenu, R.drawable.food_menu);
	   Appinfo app_gohealthy = new Appinfo(R.string.app_healthcare, R.drawable.gohealthy);
	   Appinfo app_soffers = new Appinfo(R.string.app_soffers, R.drawable.specialoffer);
	   Appinfo app_tradepoint = new Appinfo(R.string.app_trading, R.drawable.tradepoint);
	   Appinfo app_notifications = new Appinfo(R.string.app_notifications, R.drawable.notification);

	   mApps.add(app_transport);
	   mApps.add(app_tastit);
	   mApps.add(app_gohealthy);
	   mApps.add(app_soffers);
	   mApps.add(app_tradepoint);
	   mApps.add(app_notifications);
   }

   public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
	   if(AccountLogin.DEBUG) Log.i(TAG,"position:"+position);
	   
	   switch(position){
	   case TRANSPORT:
		   Intent intent = new Intent();
		   intent.setClass(this, TransportHome.class);
		   startActivity(intent);
		   break;
	   }
	}

}
